<?php
include_once "xAP-functions/webincludes.php";
$id=intval($_GET["id"]);
// setup?
if (isset($_GET["set"])){
	dbexec("update xAP_EventElement set EventLibraryID=".intval($_GET["set"])." where EventElementID=".$id); 
}

//get element details
$results=dbquery("select * from xAP_EventElement where EventElementID=".$id);
$e=$results[0];

if(intval($e["EventLibraryID"])==0){
	// no mode set yet  so list 'em so use  can pick
	$results=dbquery("select EventLibraryID,Alias,Description from xAP_EventLibrary order by Alias");
	
	foreach($results as $key=>$value){
	
		$body.="<span class='normaltext'>
		<a href='#' OnClick='ElementFromLibrary(".$id.",".$value["EventLibraryID"].")'>".$value["Alias"]."</a></span>
		<BR>
		<span class='normalsmalltext'>".$value["Desc"]."</span>";
	
	}

}
else
{
	//library is already set so display form
	$results=dbquery("select * from xAP_EventLibrary where EventLibraryID=".$e["EventLibraryID"]);
	$l=$results[0];
	
	$body.="<span class='normaltext'>".$l["Alias"]."</span><BR>
	<span class='normalsmalltext'>".$l["Description"]."</span><BR>
	";
	
	$body.="<span class='normaltext'>
	<form id='elementform'>
	<input type='hidden' name='id' value=$id>
	<table>
	<TR><TD>Target</TD>
	<TD><input name='target' size=30 value=\"".$e["Target"]."\"></TD>
	</TR>";
	
	
	
	for($i=1;$i<9;$i++){
	
		$body.=DisplayLine($l["Data".$i],$e["Data".$i],$i);
	}
	$body.="<TR><TD colspan=2><input type='button' value='Update' OnClick='UpdateElement($id)'></TD></TR></table></form></span>";
}

function DisplayLine($lne,$data,$item){
	// display the suitable line from the library data
	if ($lne!=""){
		$nme=$lne;
		if(strpos($nme,"[")>0){
			$nme=substr($nme,0,strpos($nme,"["));
			$choices=substr($lne,strpos($lne,"[")+1); //get coices
			$default="";
			if(substr($choices,-1)=="]"){$choices=substr($choices,0,strlen($choices)-1);} // strip last ]
			if(strpos($choices,":")>0){// get defaul vaule if its there
				$default=substr($choices,strpos($choices,":")+1);
				$choices=substr($choices,0,strpos($choices,":"));
				}; 
			$choicelist=explode(",",$choices);
			
			$choice="<select name='data$item'>";
				$a=0;
				foreach($choicelist as $v){
				
					if( ($v==$default && empty($data) && $default!="" && $a==0)||($v==$data && $a==0) ){
						$choice.="<option SELECTED>".$v;
						$a=1;
					}else{
						$choice.="<option>".$v;
					}
				}
			$choice.="</select>";
		}else{
			$choice="<input size=30 name='data$item'  value=\"".$data."\">";
		}
	
		$tmp="<TR><TD>".$nme."</TD><TD>".$choice."</TD></TR>";
	}
	return $tmp;
}
?>
<span  class='boldtext'><?=$e["Alias"]?></span><BR><BR>
<span class='normaltext'><?=$body?></span>